package com.gsk.europepharma.salesReport.struts.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.Session;

import com.gsk.europepharma.salesReport.hibernate.tbSpBoSRAdmPdt;
import com.gsk.europepharma.salesReport.hibernate.tbSpBoSRAdmPrstn;
import com.gsk.europepharma.salesReport.hibernate.tbSpBoSRAdmRgpt;
import com.gsk.europepharma.salesReport.hibernate.dao.tbSpBoSRAdmPdtDAO;

public class DeleteProduit extends Action {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		HashMap<String, String> hm = new HashMap<String, String>();
        
        try {
        	// VARIABLES
        	HttpSession sessionObject = request.getSession();
        	Session session;
        	tbSpBoSRAdmPdtDAO admPdtDao;
        	Query req;
			ArrayList result;
			Iterator iter;
        	
        	int cs_adm_pdt = Integer.parseInt(sessionObject.getAttribute("cs_adm_pdt").toString());
        	if (cs_adm_pdt != 0) {
        		admPdtDao = new tbSpBoSRAdmPdtDAO();
				session = admPdtDao.getSession();
				
				// IDS PRESENTATION A DETRUIRE
				List<Integer> listPrstnId = new ArrayList<Integer>();
				// IDS REGROUPEMENT A DETRUIRE
				List<Integer> listRgptId = new ArrayList<Integer>();
        		
				// SUPPRESSION DES PRESENTATIONS ASSOCIEES AU PRODUIT
				admPdtDao = new tbSpBoSRAdmPdtDAO();
				session = admPdtDao.getSession();
				req = session.getNamedQuery(admPdtDao.QUERY_LISTE_PRESENTATIONS_PAR_PRODUIT);
				req.setInteger("cs_adm_pdt", cs_adm_pdt);
				result = new ArrayList(req.list());
				iter = result.iterator();
				// Liste des id des presentations à supprimer 
				while (iter.hasNext()) {
					Object tmp = iter.next();
					listPrstnId.add(Integer.parseInt(tmp.toString()));
				}
				// Suppression des presentations 
				for (Integer idPrstn : listPrstnId) {
					tbSpBoSRAdmPrstn tbSpBoSRAdmPrstn = (com.gsk.europepharma.salesReport.hibernate.tbSpBoSRAdmPrstn) session.load(com.gsk.europepharma.salesReport.hibernate.tbSpBoSRAdmPrstn.class, idPrstn);
					session.delete(tbSpBoSRAdmPrstn);
					session.flush();
				}
				
				// SUPPRESSION DES REGROUPEMENTS ASSOCIES AU PRODUIT
				admPdtDao = new tbSpBoSRAdmPdtDAO();
				session = admPdtDao.getSession();
				req = session.getNamedQuery(admPdtDao.QUERY_LISTE_REGROUPEMENTS_PAR_PRODUIT);
				req.setInteger("cs_adm_pdt", cs_adm_pdt);
				result = new ArrayList(req.list());
				iter = result.iterator();
				// Liste des id des regroupements à supprimer 
				while (iter.hasNext()) {
					Object tmp = iter.next();
					listRgptId.add(Integer.parseInt(tmp.toString()));
				}
				// Suppression des regroupements  
				for (Integer idRgpt : listRgptId) {
					tbSpBoSRAdmRgpt tbSpBoSRAdmRgpt = (com.gsk.europepharma.salesReport.hibernate.tbSpBoSRAdmRgpt) session.load(com.gsk.europepharma.salesReport.hibernate.tbSpBoSRAdmRgpt.class, idRgpt);
					session.delete(tbSpBoSRAdmRgpt);
					session.flush();
				}
				
        		// DELETE PRODUIT 
				tbSpBoSRAdmPdt tbSpBoSRAdmPdt = (com.gsk.europepharma.salesReport.hibernate.tbSpBoSRAdmPdt) session.load(com.gsk.europepharma.salesReport.hibernate.tbSpBoSRAdmPdt.class, cs_adm_pdt);
				// Destruction en base
				session.delete(tbSpBoSRAdmPdt);
				session.flush();
        	}
        	
        	hm.put("success", "success");
        } catch (Exception e) {
    		hm.put("success", "error");
		}
		
		JSONObject json = JSONObject.fromObject(hm);
		response.addHeader("Content-Type", "application/json");
        response.getOutputStream().write(json.toString().getBytes("UTF-8"));
        response.getOutputStream().flush();
        return null;
	}

}
